package cn.itcast.order.controller;

import cn.itcast.feign.client.ItemClient;
import cn.itcast.feign.client.UserClient;
import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.hmall.pojo.order.Order;
import cn.itcast.hmall.pojo.order.OrderDetail;
import cn.itcast.hmall.pojo.order.OrderLogistics;
import cn.itcast.hmall.pojo.user.Address;
import cn.itcast.hmall.pojo.user.User;
import cn.itcast.order.mapper.OrderDetailMapper;
import cn.itcast.order.mapper.OrderLogisticsMapper;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.service.OrderDetailService;
import cn.itcast.order.service.OrderLogisticsService;
import cn.itcast.order.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("pay")
public class PayController {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderLogisticsService orderLogisticsService;
    @Autowired
    OrderLogisticsMapper orderLogisticsMapper;
    @Autowired
    UserClient userClient;
    @Autowired
    ItemClient itemClient;

    @PutMapping("/{orderId}")
    public ResultDTO pay(@PathVariable Long orderId, @RequestBody Map<String, String> map) {
        Order order = orderMapper.selectById(orderId);
        if (order==null){
            return ResultDTO.error("订单支付超时，已取消！");
        }
        if (order.getStatus() == 1) {
            User user = userClient.findById(2l);
//            Set<String> key = map.keySet();
            String value = map.get("password");
            System.out.println(value);
            //前端传过来的明文密码加密
            String passwordMd5 = DigestUtils.md5DigestAsHex(value.getBytes());
            String password = user.getPassword(); //远程查回来的密码
            if (StringUtils.isNotBlank(passwordMd5) && passwordMd5.equals(password)) {
                order.setStatus(2);
                int i = orderMapper.updateById(order);
                if (i != 0) {
                    System.out.println("success");
                }
                Address add = userClient.findAdd(59l);
                OrderLogistics orderLogistics = new OrderLogistics();
                orderLogistics.setOrderId(orderId);
                orderLogistics.setCity(add.getCity());
                orderLogistics.setTown(add.getTown());
                orderLogistics.setStreet(add.getStreet());
                orderLogistics.setCreateTime(new Date());
                orderLogistics.setUpdateTime(new Date());
                Long totalFee = order.getTotalFee();
                orderLogistics.setMobile(add.getMobile());
                orderLogistics.setProvince(add.getProvince());
                orderLogistics.setContact(add.getContact());
                user.setBalance(user.getBalance() - totalFee);
                boolean save = orderLogisticsService.save(orderLogistics);
                if (save == true) {
                    System.out.println("物流生成成功");
                }
                OrderDetail orderDetail = orderDetailMapper.selectByOrderId(orderId);
                itemClient.stock(orderDetail.getItemId(),orderDetail.getNum());
                boolean update1 = userClient.update(user);
                if (update1 == true) {
                    return ResultDTO.ok();
                }
            }
        }
        return ResultDTO.error();

    }
}
